package com.leather.market.controller;

import com.leather.common.entity.GoodsCar;
import com.leather.common.entity.User;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.ImageKit;
import com.leather.market.feginclient.UserClient;
import com.leather.market.service.GoodsService;
import com.leather.market.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserClient userClient;

    @PostMapping("add")
    public Result addGoods(MultipartFile image, GoodsVo goodsVo) {
        String goodsImgUrl = "";
        if (image != null) {
            goodsImgUrl = ImageKit.uploadImage(image, "market");
        }
        goodsVo.setGoodsImgUrl(goodsImgUrl);
        return goodsService.addGoods(goodsVo);
    }


    @GetMapping("index")
    public Result index(int pageId) {
        return goodsService.queryIndex(pageId*10);
    }

    @GetMapping("usercollection")
    public Result userCollection(int userId) {
        return goodsService.userCollection(userId);
    }

    @GetMapping("usercenter")
    public Result userCenter(int userId) {
        return goodsService.userCenter(userId);
    }

    @GetMapping("search")
    public Result search(String searchValue) {
        return goodsService.search(searchValue);
    }

    @PostMapping("setflag")
    public Result setFlag(int goodsId, int userId) {
        return goodsService.setFlag(goodsId, userId);
    }
    @PostMapping("delete")
    public Result delete(int goodsId, int userId) {
        return goodsService.delete(goodsId, userId);
    }

    @PostMapping("addcar")
    public Result addCar(GoodsCar goodsCar){
        return goodsService.addCar(goodsCar);
    }

    @PostMapping("removecar")
    public Result removeCar(GoodsCar goodsCar) {
        return goodsService.removeCar(goodsCar);
    }

    @PostMapping("judge")
    public Result judge(GoodsCar goodsCar) {
        return goodsService.judgeIsAdd(goodsCar);
    }

    @PostMapping("getphone")
    public Result getPhone(int userId){
        User user = userClient.getUserById(userId);
        if(null == user) {
            return Result.error(CodeMsg.SERVER_OFFLINE);
        }
        return Result.success(user.getPhone());
    }

    @PostMapping("updateprice")
    public Result updatePrice(int userId,int goodsId,double price){
        return goodsService.updatePrice(userId, goodsId, price);

    }

    @GetMapping("typeindex")
    public Result getType(int typeId) {
        return goodsService.getByType(typeId);
    }
}
